package com.space.school.controller;

import com.space.school.domain.ProductInfo;
import com.space.school.domain.User;
import com.space.school.service.ProductInfoService;
import com.space.school.service.UserService;
import com.space.school.service.impl.ProductInfoServiceImpl;
import com.space.school.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "productInfoOperateServlet", value = "/productInfoOperateServlet")
public class ProductInfoOperateServlet extends HttpServlet {
    ProductInfoService productInfoService = new ProductInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String product_id = req.getParameter("product_id");
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProduct_id(Integer.parseInt(product_id));
        String operate = req.getParameter("operate");
        if (operate.equals("del")){
            productInfoService.delete(productInfo);
            resp.sendRedirect("/productInfoByPageServlet");
        }else if (operate.equals("update")){
            List<ProductInfo> productInfos = productInfoService.findById(Integer.parseInt(product_id));
            req.setAttribute("product_info", productInfos.get(0));
            req.getRequestDispatcher("updateProductList.jsp").forward(req, resp);
        }else if (operate.equals("product_status")){
            String product_status = req.getParameter("product_status");
            productInfoService.updateStatus(Integer.parseInt(product_id),Integer.parseInt(product_status));
            resp.sendRedirect("/productInfoByPageServlet");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String product_id = req.getParameter("product_id");
        String product_name = req.getParameter("product_name");
        String product_price = req.getParameter("product_price");
        String product_stock = req.getParameter("product_stock");
        String product_description = req.getParameter("product_description");
        String category_type = req.getParameter("category_type");
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProduct_id(Integer.parseInt(product_id));
        productInfo.setProduct_name(product_name);
        productInfo.setProduct_price(Double.parseDouble(product_price));
        productInfo.setProduct_stock(Integer.parseInt(product_stock));
        productInfo.setProduct_description(product_description);
        productInfo.setCategory_type(category_type);
        productInfoService.update(productInfo);
        resp.sendRedirect("/productInfoByPageServlet");
    }
}
